package run;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import vkmc.Settings;

@SuppressWarnings("serial")
public class SettingsForm extends JDialog
{	
	public SettingsForm instance;
	
	private JLabel lblPath;
	private JLabel lblLang;
	private JLabel lblMinBitrate;
	
	private JTextField txtPath;
	private JComboBox<String> txtLang;
	private JTextField txtMinBitrate;
	
	private JCheckBox chkLookAndFeel;
	
	private JButton btnOk;
	private JButton btnCancel;
	private JButton btnBrowse;
	
	private JLabel lblRestart;
	
	private JFileChooser fdialog;
	
	private JTextField txtUsername;
	private JPasswordField txtPassword;
	
	private JLabel lblAdvanced;
	private JLabel lblUsername;
	private JLabel lblPassword;
	
	
	
	public SettingsForm(Container parent)
	{
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setSize(435, 290);
		setResizable(false);
		setTitle(LanguageLoader.currentLang.language.settingsGui.title);
		setLocation(Math.abs((getWidth() - parent.getWidth()) / 2), Math.abs((getHeight() - parent.getHeight()) / 2));
		setLocationRelativeTo(parent);
		setLayout(null);
		setModal(true);
		instance = this;
		
		initComponents();
		
		loadData();
		
		setVisible(true);
	}
	
	public void initComponents()
	{		
		fdialog = new JFileChooser();
		fdialog.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY);
		
		lblPath = new JLabel(LanguageLoader.currentLang.language.settingsGui.downloadPath + ": ");
		lblPath.setBounds(5, 5, 150, 25);
		add(lblPath);
		
		lblLang = new JLabel(LanguageLoader.currentLang.language.settingsGui.language + ": ");
		lblLang.setBounds(5, 35, 150, 25);
		add(lblLang);
		
		lblMinBitrate = new JLabel(LanguageLoader.currentLang.language.settingsGui.minimumBitrate + ": ");
		lblMinBitrate.setBounds(5, 65, 150, 25);
		add(lblMinBitrate);
		
		txtPath = new JTextField();
		txtPath.setBounds(155, 5, 180, 25);
		add(txtPath);
		
		btnBrowse = new JButton(LanguageLoader.currentLang.language.settingsGui.browse);
		btnBrowse.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if (fdialog.showOpenDialog(instance) == JFileChooser.APPROVE_OPTION)
					txtPath.setText(fdialog.getSelectedFile().getPath());
			}
		});
		btnBrowse.setBounds(340, 5, 90, 25);
		add(btnBrowse);
		
		txtLang = new JComboBox<String>(LanguageLoader.getLanguageNames());
		txtLang.setBounds(155, 35, 265, 25);
		add(txtLang);
		
		txtMinBitrate = new JTextField();
		txtMinBitrate.setBounds(155, 65, 265, 25);
		add(txtMinBitrate);
		
		chkLookAndFeel = new JCheckBox(LanguageLoader.currentLang.language.settingsGui.lookAndFeel);
		chkLookAndFeel.setBounds(5, 95, 265, 25);
		add(chkLookAndFeel);
		
		btnOk = new JButton(LanguageLoader.currentLang.language.settingsGui.ok);
		btnOk.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				File f = new File(txtPath.getText());
				if (!f.exists())
				{
					MsgBox.show(LanguageLoader.currentLang.language.settingsGui.invalidPath);
					return;
				}
				
				String br = txtMinBitrate.getText().trim();
				if (!std.isNumeric(br))
				{
					MsgBox.show(LanguageLoader.currentLang.language.settingsGui.invalidBitrate);
					return;
				}
				
				int _br = std.toint(br);
				
				if (_br < 96 || _br > 512)
				{
					MsgBox.show(LanguageLoader.currentLang.language.settingsGui.invalidBitrateRange);
					return;
				}
				
				if (txtUsername.getText().trim().equals(""))
				{
					MsgBox.show(LanguageLoader.currentLang.language.settingsGui.invalidDataMsg);
					return;
				}
				
				if (new String(txtPassword.getPassword()).trim().equals(""))
				{
					MsgBox.show(LanguageLoader.currentLang.language.settingsGui.invalidDataMsg);
					return;
				}
				
				storeData();
				dispose();
			}
		});
		btnOk.setBounds(getWidth() - 155, 225, 70, 30);
		add(btnOk);
		
		btnCancel = new JButton(LanguageLoader.currentLang.language.settingsGui.cancel);
		btnCancel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				dispose();
			}
		});
		btnCancel.setBounds(getWidth() - 80, 225, 70, 30);
		add(btnCancel);
		
		lblRestart = new JLabel(LanguageLoader.currentLang.language.settingsGui.restart);
		lblRestart.setFont(new Font("Sans", Font.PLAIN, 11));
		lblRestart.setForeground(Color.red.darker());
		lblRestart.setBounds(5, 235, getWidth() - 155, 30);
		add(lblRestart);
		
		lblAdvanced = new JLabel(LanguageLoader.currentLang.language.settingsGui.connection);
		lblAdvanced.setFont(new Font("Arial", Font.BOLD, 13));
		lblAdvanced.setBounds(5, 130, 275, 25);
		add(lblAdvanced);
		
		lblUsername = new JLabel(LanguageLoader.currentLang.language.settingsGui.username);
		lblUsername.setBounds(5, 160, 150, 25);
		add(lblUsername);
		
		txtUsername = new JTextField();
		txtUsername.setBounds(155, 160, 265, 25);
		add(txtUsername);
		
		lblPassword = new JLabel(LanguageLoader.currentLang.language.settingsGui.password);
		lblPassword.setBounds(5, 190, 150, 25);
		add(lblPassword);
		
		txtPassword = new JPasswordField();
		txtPassword.setBounds(155, 190, 265, 25);
		add(txtPassword);
	}
	
	private void storeData()
	{
		String data = "";
		data = String.format(Settings.default_sets, txtPath.getText(), (String)txtLang.getSelectedItem(), txtMinBitrate.getText(), (chkLookAndFeel.isSelected() ? "true" : "false"), Settings.rot13(new String(txtPassword.getPassword())), Settings.rot13(txtUsername.getText()));
		TextFileStream file = new TextFileStream(Settings.getSettingsPath());
		file.clear();
		file.addLine(data);
		file.apply();
		Settings.reload();
	}
	
	private void loadData()
	{
		txtPath.setText(Settings.getDownloadDirectory());
		txtLang.setSelectedItem(Settings.getLanguage());
		txtMinBitrate.setText(Settings.getMinBitrate());
		chkLookAndFeel.setSelected(Settings.isSystemLookAndFeel());
		txtUsername.setText(Settings.getUsername());
		txtPassword.setText(Settings.getPassword());
	}
}
